import React, { useEffect, useState, createContext, useContext, ReactNode } from "react";
import { useAnchorWallet, useConnection } from "@solana/wallet-adapter-react";
import { WalletMultiButton } from '@solana/wallet-adapter-react-ui';
import { Idl, Program, AnchorProvider, setProvider ,web3 } from "@coral-xyz/anchor";
import idl from './utils/idl.json'
import { config } from "./utils/config";

import { Connection, PublicKey } from "@solana/web3.js";
class ReadOnlyWallet {
  constructor(public readonly publicKey: PublicKey) {}

  async signTransaction(transaction: any) {
    return transaction;
  }

  async signAllTransactions(transactions: any[]) {
    return transactions;
  }
}

// 创建 WorkspaceContext
const WorkspaceContext = createContext<any>(null);

export const WorkspaceProvider = ({ children }:{ children: ReactNode }) => {
  const { connection } = useConnection();
  const wallet = useAnchorWallet();
  const moniwallet = new ReadOnlyWallet(new PublicKey('11111111111111111111111111111111'));
  const [workspace, setWorkspace] = useState<any>(null);
  const needLogin = false;
  useEffect(() => {


    // if (wallet && connection && !workspace) {
    //   const provider = new AnchorProvider(connection, wallet, {})
    //   setProvider(provider);
    //   const programId = config.programId
    //   const program = new Program(idl as Idl,programId);
    //   const needLogin = true;
    //   console.log(needLogin)
    //   setWorkspace({ wallet, connection, provider, program ,needLogin});
    // }else if(!wallet && connection && !workspace){
    //   const moniwallet = new ReadOnlyWallet(new PublicKey('11111111111111111111111111111111'));
    //   const provider = new AnchorProvider(connection,moniwallet,{});
    //   setProvider(provider);
    //   const programId = config.programId
    //   const program = new Program(idl as Idl, programId);
    //   const needLogin = false;
    //   setWorkspace({ wallet, connection, provider, program ,needLogin});
    // }

    if(wallet){
      if(!workspace.wallet){
        const provider = new AnchorProvider(connection, wallet, {})
        setProvider(provider);
        const programId = config.programId
        const program = new Program(idl as Idl,programId);
        setWorkspace({ wallet, connection, provider, program});

      }

    }else{
      if(!workspace){
        const provider = new AnchorProvider(connection, moniwallet, {})
        setProvider(provider);
        const programId = config.programId
        const program = new Program(idl as Idl, programId);
        setWorkspace({ wallet, connection, provider, program});
      }
      if(workspace && workspace.wallet){
        const provider = new AnchorProvider(connection, moniwallet, {})
        setProvider(provider);
        const programId = config.programId
        const program = new Program(idl as Idl, programId);
        setWorkspace({ wallet, connection, provider, program});
      }

    }

    // if(wallet){
    //   if(needLogin){
    //     if(!workspace){
    //       const provider = new AnchorProvider(connection, wallet, {})
    //       setProvider(provider);
    //       const programId = config.programId
    //       const program = new Program(idl as Idl,programId);
    //       const needLogin = true;
    //       console.log(needLogin)
    //       setWorkspace({ wallet, connection, provider, program ,needLogin});
    //     }
    //   }else{
    //     const provider = new AnchorProvider(connection, wallet, {})
    //     setProvider(provider);
    //     const programId = config.programId
    //     const program = new Program(idl as Idl,programId);
    //     const needLogin = true;
    //     console.log(needLogin)
    //     setWorkspace({ wallet, connection, provider, program ,needLogin});
    //   }

    // }else{
    //   if(needLogin){

    //     const provider = new AnchorProvider(connection, moniwallet, {})
    //     setProvider(provider);
    //     const programId = config.programId
    //     const program = new Program(idl as Idl,programId);
    //     const needLogin = true;
    //     console.log(needLogin)
    //     setWorkspace({ wallet, connection, provider, program ,needLogin});
    //   }else{
    //     if(!workspace){
    //       const provider = new AnchorProvider(connection, moniwallet, {})
    //       setProvider(provider);
    //       const programId = config.programId
    //       const program = new Program(idl as Idl,programId);
    //       const needLogin = true;
    //       console.log(needLogin)
    //       setWorkspace({ wallet, connection, provider, program ,needLogin});

    //     }
    //   }


    // }

  }, [wallet,connection]);

  if (!wallet) {
    // return <div style={{
    //   display:'flex',
    //   width: '100vw',
    //   height: '100vh',
    //   justifyContent: 'center',
    //   alignItems: 'center'
    // }}><WalletMultiButton/></div>
  }

  return (
    <WorkspaceContext.Provider value={workspace}>
      {children}
    </WorkspaceContext.Provider>
  );
};

// 自定义 Hook 用于访问 WorkspaceContext
export const useWorkspace = () => {
  return useContext(WorkspaceContext);
};
